package cn.cloudtogo.server.module.datasource;

import cn.cloudtogo.common.DatasourceCreateService;
import cn.cloudtogo.common.DatasourceDeleteService;
import cn.cloudtogo.common.DatasourcePutService;
import cn.cloudtogo.common.DatasourceQueryService;
import cn.cloudtogo.common.types.DatasourceCreateObject;
import cn.cloudtogo.common.types.Pair;
import cn.cloudtogo.common.types.Result;
import cn.cloudtogo.common.util.JdbcConnectionHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static cn.cloudtogo.server.WebResponse.accepted;
import static cn.cloudtogo.server.WebResponse.ok;

/**
 * @author yaolianhua789@gmail.com
 **/
@RestController
@RequestMapping("/datasources")
public class ExternalDatasourceController {

    private final DatasourceCreateService datasourceCreateService;
    private final DatasourceQueryService datasourceQueryService;
    private final DatasourceDeleteService datasourceDeleteService;
    private final DatasourcePutService datasourcePutService;

    public ExternalDatasourceController(DatasourceCreateService datasourceCreateService,
                                        DatasourceQueryService datasourceQueryService,
                                        DatasourceDeleteService datasourceDeleteService,
                                        DatasourcePutService datasourcePutService) {
        this.datasourceCreateService = datasourceCreateService;
        this.datasourceQueryService = datasourceQueryService;
        this.datasourceDeleteService = datasourceDeleteService;
        this.datasourcePutService = datasourcePutService;
    }

    @PostMapping
    public ResponseEntity<Result<String>> post(@Valid @RequestBody DatasourceCreateObject createObject) {
        return accepted(datasourceCreateService.create(createObject));
    }

    @PostMapping("/validation")
    public ResponseEntity<Result<Boolean>> validation(@RequestParam("url") String url) {
        return ok(JdbcConnectionHelper.isValidConnection(url));
    }

    @GetMapping
    public ResponseEntity<Result<List<Pair>>> datasources(@RequestParam String openid) {
        List<Pair> pairs = datasourceQueryService.list(openid);
        return ok(pairs);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Result<Pair>> datasource(@PathVariable String id) {
        Pair pair = datasourceQueryService.query(id);
        return ok(pair);
    }

    @DeleteMapping
    public ResponseEntity<Result<Void>> deletes(@RequestParam String openid) {
        datasourceDeleteService.deletes(openid);
        return accepted();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Void>> delete(@PathVariable String id) {
        datasourceDeleteService.delete(id);
        return accepted();
    }

    @PutMapping
    public ResponseEntity<Result<Void>> updateConnectionInfo(@RequestParam String openid,
                                                             @RequestParam String url) {
        datasourcePutService.updateConnectionInfoByOpenid(openid, url);
        return accepted();
    }

    @PutMapping("/{id}")
    public ResponseEntity<Result<Void>> updateName(@PathVariable String id,
                                                   @RequestParam("component") String name) {
        datasourcePutService.updateNameByUuid(id, name);
        return accepted();
    }

}
